const name = '缓存'
const modules = {}

// const files = require.context('./consumption-modules', false, /\.js$/)
// files.keys().forEach(key => {
//   modules[key.replace(/(\.\/|\.js)/g, '')] = files(key).default
// })

export default {
  namespaced: true,
  modules: modules,
  state: {
    __prefix: '',
    __name: name,

    keepAliveComponents: []
  },

  getters: {},

  mutations: {
    addToKeepAlive(state, componentName) {
      if (!state.keepAliveComponents.includes(componentName)) {
        state.keepAliveComponents.push(componentName)
      }
    },
    clearKeepAlive(state, componentName) {
      const index = state.keepAliveComponents.indexOf(componentName)
      if (index !== -1) {
        state.keepAliveComponents.splice(index, 1)
      }
    }
  },

  actions: {
    __autoload({ dispatch, state }, prefix) {
      state.__prefix = prefix + '/'
      console.log(`[store][${state.__prefix}${state.__name}] 初始化`)

      Object.keys(modules).forEach(key => {
        if (!!modules[key]['actions'] && !!modules[key]['actions']['__autoload']) {
          dispatch(`${key}/__autoload`, state.__prefix + state.__name)
        }
      })
    }
  }
}
